/**
 * Created by Administrator on 2018/11/29.
 */
$CertusJsonAjax.ajax("borrowById",{
    "processInstanceId": $CertusJsonAjax.getProInstanceId(),
    "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
},function(res){
    $$scope.borrow = {}
    $$scope.borrow.personVisible = true;
    //personal select
    $$scope.personalSelect = [
        {name:"个人",code:"personal"},
        {name:"项目",code:"project"}
    ]

    $$scope.wkStepName = $CertusJsonAjax.getWkStepName();

    $$scope.borrow = res.data;

    if($$scope.borrow.borrowingType == "personal"){
        $$scope.borrow.personVisible = true;
    }else{
        $$scope.borrow.personVisible = false;
    }

    $$scope.processs = res.data.processs;
    $$scope.processCCBXTrace = res.data.processs;
    $$scope.borrowStep = {
        "processInstanceId": $CertusJsonAjax.getProInstanceId(),
        "stepInstanceId": $CertusJsonAjax.getStepInstanceId()
    }
})

var pickerCA = $('#datetimeCA').datetimepicker({
    format: 'YYYY-MM-DD',
    locale: moment.locale('zh-cn')
}).on('dp.change', function (e) {
    var result = new moment(e.date).format('YYYY-MM-DD');
    if (result && !angular.isUndefined(result)) {
        $$scope.borrow.realRemitDate = result;
        $$scope.$apply();
    }
})

$$scope.personChange = function(){
    if($$scope.borrow.borrowingType == "personal"){
        $$scope.borrow.borrowingName = "个人";
        $$scope.borrow.personVisible = true;
    }else{
        $$scope.borrow.borrowingName = "项目";
        $$scope.borrow.personVisible = false;
    }
}

$$scope.$watch("borrow.projName", function (newValue, old, scope) {
    if (old && newValue) {
        if (newValue.indexOf(old) > -1) {
            nowProjName = newValue;
            scope.userProjSelect2 = scope.userProjSelect2.filter(isSaveProjName);
            if (scope.userProjSelect2.length) {
                if (scope.userProjSelect2.length == 1) {
                    scope.borrow.projId = scope.userProjSelect2[0].projId;
                    scope.borrow.projName = scope.userProjSelect2[0].projName;
                }
            }
            if(!scope.userProjSelect2 || scope.userProjSelect2.length == 0){
                scope.borrow.projId = "";
                scope.borrow.projName = "";
            }
            return;
        }
    }

    //直接去数据库 查询
    $CertusFormAjax.ajax("relatedProj", {param: newValue}, function (res) {
        scope.userProjSelect2 = res.data;
        if (scope.userProjSelect2) {
            if (scope.userProjSelect2.length == 1) {
                scope.borrow.projId = scope.userProjSelect2[0].projId;
                scope.borrow.projName = scope.userProjSelect2[0].projName;
            }
        }else if(!scope.userProjSelect2 || scope.userProjSelect2.length == 0){
            scope.borrow.projId = "";
            scope.borrow.projName = "";
        }
    })
})

$$scope.getsssbbb = function(bb){
    ;
    //starting denied sentback others completed
    if(bb == "starting"){
        return "正在进行中";
    }else if(bb == "denied"){
        return "否决";
    }else if(bb == "sentback"){
        return "被退";
    }else if(bb == "others"){
        return "关闭";
    }else if(bb == "completed"){
        return "完成";    }else if(bb == "terminated"){        return "终结";    }    else if(bb == "cancelled"){        return "通过";}
    return "待办";
}

//继续提交
$$scope.borrowApproval = function(){
    if(certusValidateNotnull()){
        $CertusJsonAjax.ajax("borrowApprovalEnd",$$scope.borrowStep,function(res){
            if(res.data == "success"){
                findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());
                history.back();
            }
        })
    }

}

$$scope.borrowingDenied = function(){
    $CertusJsonAjax.ajax("borrowingDenied",$$scope.borrowStep,function(res){
        // findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());.
        layer.msg(res.data);
        history.back();
    })
}

$$scope.shutDown = function(){
    $CertusJsonAjax.ajax("shutDownBorrow",$$scope.borrowStep,function(res){
        // findBacklogPerson($CertusFormAjax,$CertusJsonAjax.getStepInstanceId());.
        layer.msg("已经否决");
        history.back();
    })
}

$$scope.borrowSubmitAgain = function(){
    if(certusValidateNotnull()){
        $$scope.borrow.tmpStepId = $CertusJsonAjax.getStepInstanceId();
        $$scope.borrow.tmpProcessId = $CertusJsonAjax.getProInstanceId();

        if($$scope.borrow.borrowingType != "personal"){
            if($$scope.borrow.projId){

                $CertusJsonAjax.ajax("borrowStart",$$scope.borrow,function(res){
                    if(res.data == "success"){
                        findBacklogPersonHead($CertusFormAjax, "JKSQLC");
                        history.back();
                    }
                })
            }else{
                layer.msg("请填写正确项目");
            }
        }else{
            $CertusJsonAjax.ajax("borrowStart",$$scope.borrow,function(res){
                if(res.data == "success"){
                    findBacklogPersonHead($CertusFormAjax, "JKSQLC");
                    history.back();
                }
            })
        }
    }
}